package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.OrderProcure;
import com.mrtan.bean.OrderSell;
import com.mrtan.dto.FinancialDto;
import com.mrtan.dto.InandOutInfoDto;
import com.mrtan.dto.AllCountAndPriceDto;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.dto.YearDto;
import com.mrtan.dto.YearsDto;
import com.mrtan.mapper.OrderMapper;
import com.mrtan.service.OrderService;
import com.mrtan.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 销售业务和采购业务
 */

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderMapper orderMapper;



    //显示仓库和库存

    //生成订单
    @PutMapping("/addsell")
    public SaResult addsell(@RequestBody OrderInsertVO orderInsertVO) {
        orderService.addSell(orderInsertVO);
        return SaResult.ok();
    }
    @PostMapping("/updateSell")
    public SaResult updateSell(@RequestBody OrderUpdateWhoVo orderUpdateWhoVo) {
        orderService.updateSell(orderUpdateWhoVo);
        return SaResult.ok();
    }

    //订单查询（包含模糊查询）
    @GetMapping("/showsell")
    public SaResult orderList(OrderShowWhereVO orderShowWhereVO,
                              @RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit); // 在查询之前使用,会自动对紧接着的第一个查询进行分页
        List<OrderSell> list = orderService.orderList(orderShowWhereVO);
        PageInfo<OrderSell> pageInfo = new PageInfo<>(list);
        PageBeanDto<OrderSell> pageBeanDto = new PageBeanDto();
        pageBeanDto.setList(pageInfo.getList());
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        return SaResult.ok()
                .setData(pageBeanDto);
    }


    @PostMapping("/showprocure")
    public SaResult orders(@RequestBody OrderProcureVO orderProcureVO) {
        System.out.println(orderProcureVO);
        PageHelper.startPage(orderProcureVO.getPage(), orderProcureVO.getLimit()); // 在查询之前使用,会自动对紧接着的第一个查询进行分页
        List<OrderProcure> list = orderService.orderProductList(orderProcureVO);
        PageInfo<OrderProcure> pageInfo = new PageInfo<>(list);
        PageBeanDto<OrderProcure> pageBeanDto = new PageBeanDto();
        pageBeanDto.setList(pageInfo.getList());
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        return SaResult.ok()
                .setData(pageBeanDto);
    }

    @PutMapping("/purchase")  // 添加采购订单
    public SaResult addPurchase(@RequestBody PurchaseOrderVO purchaseOrderVO) {
        orderService.addPurchase(purchaseOrderVO);
        return SaResult.ok();
    }

    /**
     * 财务支出
     * @return {@link SaResult}
     */
    @GetMapping("/financial/{year}")
    public SaResult financial(@PathVariable int year){
        FinancialDto financial = orderService.findFinancial(year);
        return SaResult.ok().setData(financial);
    }
    @GetMapping("/financialyear")
    public SaResult financial(){
        List<Integer> allYear= orderService.findFinancialyear();
        return SaResult.ok().setData(allYear);
    }


    @GetMapping("/{order}")
    public SaResult meho(@PathVariable int order){
        OrderSell orderSell = orderService.findbyorderId(order);
        List<InandOutInfoDto> findinanoutbyorder = orderService.findinanoutbyorder(order);

        return SaResult.ok().setData(orderSell).set("inandout",findinanoutbyorder);
    }


    @GetMapping("/getAllPrice")
    public SaResult getAllPrice() {
        BigDecimal i = orderService.getAllPrice();
        return SaResult.ok()
                .setData(i);
    }
    @GetMapping("/getAllOrderCount")
    public SaResult getAllOrderCount() {
        int i = orderService.getAllOrderCount();
        return SaResult.ok()
                .setData(i);
    }

    @GetMapping("/getAllByYearAndMonth/{year}")
    public SaResult getAllByYearAndMonth(@PathVariable String year) {
        List<YearsDto> list = orderService.findOrderCountAndPrice(year);
        AllCountAndPriceDto allCountAndPriceDto = new AllCountAndPriceDto();
        allCountAndPriceDto.setAllOrder(list);
        return SaResult.ok()
                .setData(allCountAndPriceDto);
    }

}
